package com.hf.base.shh;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;

import org.springframework.beans.factory.annotation.Autowired;
import sys.util.EntityUtil;

public class BaseServiceImpl<TForm extends BaseForm<Eentiy>, Eentiy, TDao extends BaseDao<Eentiy>>
		implements BaseService<TForm, Eentiy> {

	@Autowired
	protected TDao dao;

	protected Class<TForm> fromClass;
	protected Class<Eentiy> entityClass;

	protected String fromName;
	protected String entityName;
	protected String keyName;

	public BaseServiceImpl() {
		Type type = getClass().getGenericSuperclass();
		Type[] params = ((ParameterizedType) type).getActualTypeArguments();
		fromClass = (Class<TForm>) params[0];
		entityClass = (Class<Eentiy>) params[1];
		fromName = fromClass.getSimpleName();
		entityName = entityClass.getSimpleName();
		keyName = EntityUtil.getKeyName(entityClass);
	}

	@Override
	public TForm getPage(int pageSize, int pageNum) {
		// TODO Auto-generated method stub
		return getPage(null);
	}

	@Override
	public TForm getPage(Eentiy entity) {
		// TODO Auto-generated method stub
		String hql = " from " + entityName;
		String hqlWhere = " ";
		Map<String, Object> paramters = new HashMap<>();
		if (entity != null) {
			Class<Entity> clzz = (Class<Entity>) entity.getClass();
			Field[] fields = clzz.getDeclaredFields();
			for (Field field : fields) {
				try {
					field.setAccessible(true);
					if ((!field.getName().equals("serialVersionUID")) && (field.get(entity) != null)) {
						if (field.getType().equals(String.class)  ) {
							if (!field.get(entity).equals("")) {
								hqlWhere = hqlWhere + "and " + field.getName() + " =:" + field.getName() + " ";
								paramters.put(field.getName(), field.get(entity));								
							}

						} 
						else
						{
							hqlWhere = hqlWhere + "and " + field.getName() + " =:" + field.getName() + " ";
							paramters.put(field.getName(), field.get(entity));
						}
					}
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();

				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();

				}
			}
		}

		if (!paramters.isEmpty()) {
			hqlWhere = " where " + hqlWhere.substring(4);

		}
		hql = hql + hqlWhere;

		TForm form = null;
		try {
			form = fromClass.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(fromName + "创建失败");
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(fromName + "创建失败");

		}
		System.out.println(hql);
		List<Eentiy> list = dao.find(hql, paramters);
		form.setEntities(list);
		form.setEntity(entity);

		return form;
	}

	@Override
	public boolean saveEntity(Eentiy entiy) {
		// TODO Auto-generated method stub
		dao.saveOrUpdate(entiy);
		return true;
	}

	@Override
	public Eentiy getEntity(String key) {
		// TODO Auto-generated method stub
		if (keyName == null) {
			return null;
		}
		Map<String, Object> paramters = new HashMap<>();
		paramters.put("id", key);
		return dao.get(" from " + entityName + " where " + keyName + "=:id  ", paramters);
	}

	@Override
	public Eentiy getEntity(Eentiy entiy) {
		// TODO Auto-generated method stub
		String fieldName = EntityUtil.getKeyName((entiy.getClass()));
		String key = EntityUtil.getValue(entiy, fieldName, entiy.getClass()).toString();

		return getEntity(key);
	}

}
